package com.gxh.controller.admin;

import com.gxh.Websocket.WebSocketServer;
import com.gxh.result.Result;
import com.gxh.service.NotificationServe;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController("adminNotificationController")
@RequestMapping("/admin/notification")
@Api(tags = "管理端通知接口")
@Slf4j
public class NotificationController {

    @Autowired
    private NotificationServe notificationServe;
    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 查询缓存中所有上传的数量
     * @return
     */
    @GetMapping("/upload/count")
    @ApiOperation("查询缓存中所有上传的数量")
    public Result<Integer> count() {
        //查询缓存中所有上传的数量
        log.info("查询缓存中所有上传的数量");
        Integer count =notificationServe.uploadCount();
        return Result.success(count);
    }

    @GetMapping("/request/count")
    @ApiOperation("查询缓存中所有求购的数量")
    public Result<Integer> requestCount() {
        log.info("查询缓存中所有求购的数量");
        Integer count =notificationServe.requestCount();
        return Result.success(count);
    }

    @GetMapping
    @ApiOperation("发送消息到客户端")
    public Result sendMessage() {
        log.info("发送消息到客户端");
        //判断是否有缓存未处理
        int upload = notificationServe.uploadCount();
        int request = notificationServe.requestCount();

        if (upload > 0) {
            webSocketServer.sendToAllClient(1, null, "有 " + upload + " 条未处理的【设备上传】");
        }
        if (request > 0) {
            webSocketServer.sendToAllClient(2, null, "有 " + request + " 条未处理的【求购信息】");
        }
        return Result.success();
    }
}
